package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.CardDao;
import com.aaa.entity.Card;
import com.aaa.entity.Duo;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class CardDaoImpl implements CardDao {
    BaseDao baseDao = BaseDao.getInstance();

    @Override
    public int changeStatus(Card card) {
        String sql = "update card set status=? where cardId=?";
        Object[] objects = {card.getStatus(),card.getCardId()};
        return baseDao.executeUpdate(sql,objects);
    }

    @Override
    public List<Card> getAllCardList(Integer pageNumber, Integer pageSize, String searchId, String searchName) {
        String sql = "select " +
                " c.*,u.userName userName,ct.name cardTypeName " +
                " from " +
                " card c LEFT JOIN cardtype ct ON c.levelId=ct.level LEFT JOIN user u on c.userId=u.userId " +
                " where 1=1 ";
        if(StringUtils.isNotBlank(searchId)){
            sql += " and c.cardId = "+ searchId;
        }
        if(StringUtils.isNotBlank(searchName)){
            sql += " and u.userName like '%"+searchName+"%' ";
        }
        sql += " order by c.id asc limit ?,?";
        Object[] params = {pageNumber, pageSize};
        List<Card> list = baseDao.query(sql,params,Card.class);
        return list;
    }

    @Override
    public int getAllCardInfoCount(String searchId, String searchName) {
        String sql = "select count(1) len from user where 1=1 ";
        if (StringUtils.isNotBlank(searchId)) {
            sql += " and cardId = " + searchId;
        }
        if(StringUtils.isNotBlank(searchName)){
            searchName = "%"+searchName+"%";
            sql += " and userName like '%"+searchName+"%' ";
        }
        List<Map<String,Object>> maps = baseDao.query(sql,null);
        if(maps != null && maps.size() > 0){
            Map<String, Object> map = maps.get(0);
            Integer res = Integer.parseInt(map.get("len") + "");
            return res;
        }
        return 0;
    }

    @Override
    public List<Duo> getAllSlotCard(String searchId) {
        if(StringUtils.isNotBlank(searchId)){
        String sql = "select " +
                " c.userId,c.status cardStatus,c.cardId,c.amount,u.userName,u.phone,u.address,u.idCard,u.sex, " +
                " ct.name,o.orderId,o.price,o.pay,o.credit,o.status orderStatus,o.momo orderMomo,o.createdTime orderCareatedTime, " +
                " r.rechargeAmount,r.afterAmount,r.beforeAmount,r.ruleId,r.momo rMomo,r.createdTime rCreatedTime " +
                " from " +
                " card c,user u,cardtype ct,rechargerecord r,`order` o " +
                " where " +
                " c.cardId=u.cardId " +
                " AND c.cardId=r.cardId " +
                " AND c.cardId=o.cardId " +
                " AND c.levelId=ct.level ";

            sql += " and c.cardId = "+ searchId;
            List<Duo> list = baseDao.query(sql,null,Duo.class);
            return list;
        }else {
            return null;
        }
    }

    @Override
    public List<Duo> getAllSlotCardList(Integer pageNumber, Integer pageSize, String searchId) {
        if(StringUtils.isNotBlank(searchId)){
        String sql = "select " +
                " c.userId,c.status cardStatus,c.cardId,c.amount,u.userName,u.phone,u.address,u.idCard,u.sex, " +
                " ct.name,o.orderId,o.price,o.pay,o.credit,o.status orderStatus,o.momo orderMomo,o.createdTime orderCareatedTime, " +
                " r.rechargeAmount,r.afterAmount,r.beforeAmount,r.ruleId,r.momo rMomo,r.createdTime rCreatedTime " +
                " from " +
                " card c,user u,cardtype ct,rechargerecord r,`order` o " +
                " where " +
                " c.cardId=u.cardId " +
                " AND c.cardId=r.cardId " +
                " AND c.cardId=o.cardId " +
                " AND c.levelId=ct.level ";

            sql += " and c.cardId = "+ searchId;

            sql += " order by c.cardId asc limit ?,?";
            Object[] params = {pageNumber, pageSize};
            List<Duo> list = baseDao.query(sql,params,Duo.class);
            return list;
        }else{
            return null;
        }
    }

    @Override
    public int getAllSlotCardInfoCount(String searchId) {
        if (StringUtils.isNotBlank(searchId)) {
        String sql = "select count(1) len from card c,user u,cardtype ct,rechargerecord r,`order` o where 1=1 AND c.cardId=u.cardId " +
                " AND c.cardId=r.cardId " +
                " AND c.cardId=o.cardId " +
                " AND c.levelId=ct.level ";

            sql += " AND c.cardId = " + searchId;

            List<Map<String,Object>> maps = baseDao.query(sql,null);
            if(maps != null && maps.size() > 0){
                Map<String, Object> map = maps.get(0);
                Integer res = Integer.parseInt(map.get("len") + "");
                return res;
            }
        }
        return 0;
    }
}
